﻿using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using Outlook.WorkItems.Extension.Manager.TeamProjectManager;

namespace Outlook.WorkItems.Extension.Addin.Forms
{
    public partial class ManageMappingForm : Form
    {
        public ManageMappingForm()
        {
            InitializeComponent();
        }

        private void InitMappingList()
        {
            var filePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\OWIPlugin\\WorkItemMapping.owi";

            if (!File.Exists(filePath))
                return;

            XDocument xdoc = XDocument.Load(filePath);
            XElement rootElement = xdoc.Element("WorkItemMappings");

            if (rootElement == null)
                return;

            foreach (var xElement in rootElement.Elements("WorkItemMapping"))
            {
                var wim = new WorkItemMapping();
                wim.DeserializeWorkItemMapping(xElement);
                listBoxMapping.Items.Add(new WorkItemMappingListBoxItem(wim));
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            var mapping = listBoxMapping.SelectedItem;
            if (mapping == null)
                return;

            foreach (Control control in groupBoxMapping.Controls)
            {
                control.Enabled = true;
            }
            foreach (Control control in panelGeneral.Controls)
            {
                control.Enabled = false;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            // TODO : save mapping

            foreach (Control control in groupBoxMapping.Controls)
            {
                control.Enabled = false;
            }
            foreach (Control control in panelGeneral.Controls)
            {
                control.Enabled = true;
            }
        }


        private class WorkItemMappingListBoxItem
        {
            public WorkItemMapping Mapping { get; set; }

            public WorkItemMappingListBoxItem(WorkItemMapping mapping)
            {
                Mapping = mapping;
            }

            public override string ToString()
            {
                return Mapping.Name;
            }
        }

        private void ManageMappingForm_Load(object sender, EventArgs e)
        {
            InitMappingList();
        }

        private void listBoxMapping_SelectedIndexChanged(object sender, EventArgs e)
        {
            var workItemMappingListBoxItem = listBoxMapping.SelectedItem as WorkItemMappingListBoxItem;
            if (workItemMappingListBoxItem == null)
                return;

            var workItemMapping = workItemMappingListBoxItem.Mapping;

            listView1.Items.Clear();

            if (workItemMapping == null)
                return;

            foreach (var mappingField in
                workItemMapping.MappingFields)
            {
                if (!string.IsNullOrEmpty(mappingField.MessageFieldName))
                {
                    //listView1.Items.Add(new MappingListViewItem(mappingField.));

                }
                //else if (!string.IsNullOrEmpty(mappingField.StaticValue))
                //{
                //    listView1.Items.Add(
                //        new MappingListViewItem(
                //            _workItemType.FieldDefinitions[mappingField.WIFieldName], null, mappingField.StaticValue));
                //}
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            foreach (Control control in groupBoxMapping.Controls)
            {
                control.Enabled = false;
            }
            foreach (Control control in panelGeneral.Controls)
            {
                control.Enabled = true;
            }
        }
    }
}
